package com.edt.ichannel.api.service.impl;

import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.dao.StaffDao;
import com.edt.ichannel.api.entiy.local.Staff;
import com.edt.ichannel.api.service.StaffService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * Staff服务接口实现类
 * @author  孙梁
 * @date 2018-06-12
 */
@Service
public class StaffServiceImpl implements StaffService {

    @Resource
    private StaffDao staffDao;

    private Logger logger = LogManager.getLogger(StaffServiceImpl.class);


    /**
    * 获取单条信息
    *
    * @param id 拓展人id
    * @return Staff 拓展人实体
    * @author  孙梁
    * @date 2018-06-12
    */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    public Staff getStaffById(String id) {
        return staffDao.getStaffById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void addStaffAccountMoney(double money, String staffId) {
        staffDao.addStaffAccountMoney(money,staffId);
    }

    @Override

    public void minusStaffAccouintMoney(double money, String staffId) {
        staffDao.minusStaffAccountMoney(money,staffId);
    }

    @Override
    public void addStaffCommissionMoney(double addMoney, String staffId) {
        staffDao.addStaffCommissionMoney(addMoney,staffId);
    }

    @Override
    public void addStaffBonusMoney(double addMoney, String staffId) {
        staffDao.addStaffBonusMoney(addMoney,staffId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveOrUpdateStaffList(List<Staff> staffList) {
        if(staffList != null && !staffList.isEmpty()){
            List<Staff> addStaffList = new ArrayList<>();
            List<Staff> updateStaffList = new ArrayList<>();
            for (Staff staff : staffList) {
                if (getStaffById(staff.getId()) != null){
                    updateStaffList.add(staff);
                }else{
                    addStaffList.add(staff);
                }
            }
            if(!addStaffList.isEmpty()){
                staffDao.addStaffList(addStaffList);
            }
            if(!updateStaffList.isEmpty()){
                staffDao.updateStaffList(updateStaffList);
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void addStaffList(List<Staff> addMechanismsList) {
        if (addMechanismsList != null){
            staffDao.deleteAllDBlink();
            int count = CommonConstant.BRANCH_COUNT;
            int lastIndex = count ;
            for(int index = 0; index < addMechanismsList.size();){
                if(lastIndex > addMechanismsList.size()){
                    lastIndex = addMechanismsList.size();
                    staffDao.addStaffList(addMechanismsList.subList(index,lastIndex));
                    break;
                }else{
                    staffDao.addStaffList(addMechanismsList.subList(index, lastIndex));
                    index = lastIndex;
                    lastIndex = index + (count);
                }
            }
            logger.info("list 长度："+addMechanismsList.size());
        }else{
            logger.info("list为空");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updatStaffList(List<Staff> updateMechanismsList) {
        if (updateMechanismsList != null){
            int count = CommonConstant.BRANCH_COUNT;
            int lastIndex = count ;
            for(int index = 0; index < updateMechanismsList.size();){
                if(lastIndex > updateMechanismsList.size()){
                    lastIndex = updateMechanismsList.size();
                    staffDao.updateStaffList(updateMechanismsList.subList(index,lastIndex));
                    break;
                }else{
                    staffDao.updateStaffList(updateMechanismsList.subList(index, lastIndex));
                    index = lastIndex;
                    lastIndex = index + (count);
                }
            }
            logger.info("list 长度："+updateMechanismsList.size());
        }else{
            logger.info("list为空");
        }
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateExpansionMechanismsIdDifferent() {
        staffDao.updateExpansionMechanismsIdDifferent();
    }

    @Override
    public void addStaffFromStaffDblink() {
        staffDao.addStaffFromStaffDblink();
    }

    @Override
    public void mergeStaffFromStaffDblink() {
        staffDao.mergeStaffFromStaffDblink();
    }

    @Override
    public List<Staff> selectHrStaffByDBLink() {
        return staffDao.selectHrStaffByDBLink();
    }

    @Override
    public void updateAccountMoney(double accountMoney, String staffId) {
        staffDao.upDateAccountMoney(accountMoney,staffId);
    }

    @Override
    public void mergeStaffFromDblink() {
        staffDao.mergeStaffFromDblink();
    }


}